package in.muton.edushare.jobs;

import in.muton.edushare.components.tasks.electives.allotment.ChangeSem;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.List;

import org.apache.cayenne.access.DataContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.model.IModel;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.orm.CayenneORMInterface;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Universities;

public class ElectiveAssignJob implements EduShareJob {
	
	private static final Log log = LogFactory.getLog(ElectiveAssignJob.class);

	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		ORMInterface ormIntf = getORMInterface();
		List<Universities> univList = new UniversitiesDAO(ormIntf)
				.getAllUniversities();
		for (Universities univ : univList) {
			log.debug("University = "+ univ.getName());
			if(univ.getElectiveSelectionTime() == Universities.UNIV_LOCK_ELECTIVE_SELECTION){
				IModel<Universities> univModel = new CayenneModel<Universities>(
						univ);
				log.debug("Processing university = "+ univ.getName());
				new ChangeSem(univModel).setElectiveCourses(ormIntf);
				univ.setElectiveSelectionTime(Universities.UNIV_CLOSE_ELECTIVE_SELECTION);
			}
		}
		ormIntf.commitAllChanges();
	}
	
	public static ORMInterface getORMInterface(){
		CayenneORMInterface ormIntf = new CayenneORMInterface();
		ormIntf.setDataContext(DataContext.createDataContext());
		return ormIntf;
	}
}
